package com.situ.struts.action;

import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.situ.struts.entity.Admin;
import com.situ.struts.service.IAdminService;
import com.situ.struts.service.impl.AdminServiceImpl;

public class AdminAction extends ActionSupport implements ModelDriven<Admin>{
	private Admin admin = new Admin();
	
	private IAdminService adminService = new AdminServiceImpl();

	@Override
	public Admin getModel() {
		return admin;
	}
	
	public String login() {
		System.out.println("AdminAction.login()");
		Admin adminLogin = adminService.login(admin);
		//登录失败
		if (adminLogin == null) {
			return "loginFail";
		}
		//登录成功：把Admin对象放到Session中
		ActionContext actionContext = ActionContext.getContext();
		Map<String, Object> sessionMap = actionContext.getSession();
		sessionMap.put("admin", adminLogin);
		return "loginSuccess";
	}
	
	public String list() {
		Admin admin1 = new Admin(23, "zhansgan", 12);
		Admin admin2 = new Admin(23, "lisi", 12);
		Admin admin3 = new Admin(23, "wangwu", 12);
		ArrayList<Admin> list = new ArrayList<>();
		list.add(admin1);
		list.add(admin2);
		list.add(admin3);
		
		ActionContext context = ActionContext.getContext();
		Map<String, Object> requestMap = context.getContextMap();
		requestMap.put("list", list);
		
		return "list";
	}


}
